package com.swh.controller;

import com.sun.org.apache.bcel.internal.generic.NEW;
import com.swh.converter.DateConverter;
import com.swh.entity.Meeting;
import com.swh.entity.PageEntity;
import com.swh.servic.MeetingService;
import org.apache.commons.beanutils.BeanUtilsBean;
import org.apache.commons.beanutils.ConvertUtilsBean;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.crypto.Data;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

/**
 * @Author: swh
 * @Description:
 * @Date: 2020/11/25 16:50
 * @Version: 1.0
 */
@WebServlet("/meeting/*")
public class MeetingServlet extends BaseServlet {
    private MeetingService meetingService = new MeetingService();

    public void findPage(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map<String, String> param = getParam(request);

        Integer currPage = Integer.parseInt(param.get("currPage"));

        String title = param.get("title");

        //会议状态默认为-1
        Integer status = -1;

        //会议状态不能为空
        if (!"".equals(param.get("status"))) {
            status = Integer.parseInt(param.get("status"));
        }
        // 会议分页信息
        PageEntity<Meeting> pageData = meetingService.findPage(status, title, currPage);

        request.setAttribute("pageData", pageData);

        // 搜索条件回显
        request.setAttribute("title", title);
        request.setAttribute("status", status);
        request.getRequestDispatcher("/html/meeting.jsp").forward(request, response);

    }

    //添加发布add
    public void add(HttpServletRequest request, HttpServletResponse response) throws Exception {

        Map<String, String> param = getParam(request);

        Meeting meeting = new Meeting();
        
        // 定义转化器
        ConvertUtilsBean convertUtilsBean = new ConvertUtilsBean();
        convertUtilsBean.register(new DateConverter(), Date.class);

        // 用这个来自动映射
        BeanUtilsBean beanUtilsBean = new BeanUtilsBean(convertUtilsBean);

        beanUtilsBean.populate(meeting, param);


        meetingService.save(meeting);

        response.sendRedirect("/meeting/findPage?currPage=1");

    }

    //参加会议
    public void meetingDetail(HttpServletRequest request, HttpServletResponse response) throws Exception {

        Map<String, String> param = getParam(request);

        long id = Long.parseLong(param.get("id"));

        // 查询会议详情
        Meeting meeting = meetingService.findById(id);

        // 需要参加会议的用户
        String[] joinUser = meeting.getMakeUser().split(",");

        // 实到人数
        Integer realCount = meetingService.countJoinMeetingByMeetingId(id);

        // 未到人数
        Integer noCount = joinUser.length - realCount;

        // 查看我是否有参加过这个会议
        Boolean flag = meetingService.isJoinMeeting(login.getId(), id);

        request.setAttribute("meeting", meeting);
        request.setAttribute("joinCount", joinUser.length);
        request.setAttribute("realCount", realCount);
        request.setAttribute("noCount", noCount);
        request.setAttribute("flag", flag);
        // 当前登录用户是否可以参加此次会议
        request.setAttribute("isJoin", Arrays.asList(joinUser).contains(login.getId()));

        request.getRequestDispatcher("/html/meeting_detail.jsp").forward(request, response);
    }

    //参加会议
    public void joinMeeting(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map<String, String> param = getParam(request);

        Long id = Long.parseLong(param.get("id"));

        // 查询原来是否有参加过这个会议
        Boolean isJoinMeeting = meetingService.isJoinMeeting(login.getId(), id);

        if (isJoinMeeting) {
            // 说明原来参加过这个会议
            meetingService.unJoinMeeting(login.getId(), id);
        } else {
            // 说明未参加过这个会议
            meetingService.joinMeeting(login.getId(), id);
        }

        // 重定向到/meeting/meetingDetail请求
        response.sendRedirect("/meeting/meetingDetail?id="+id);

    }

}
